﻿using System.Windows;
using Principle20_20_20.ViewModel;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Forms;
using System.Windows.Threading;
using System.Diagnostics;
using System.Windows.Input;
using System;

namespace Principle20_20_20
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private NotifyIcon MyNotifyIcon;
        private DispatcherTimer _appTimer = new DispatcherTimer();

        /// <summary>
        /// Initializes a new instance of the MainWindow class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            InitializeAppTimer();
            InitializeNotifyIcon();

            MenuItem quitMenuItem = new MenuItem("Quit");
            quitMenuItem.Click += new EventHandler((o,e) => this.CloseApp());
            MyNotifyIcon.ContextMenu = new ContextMenu();
            MyNotifyIcon.ContextMenu.MenuItems.Add(quitMenuItem);

            this.Activated += new System.EventHandler(MainWindow_Activated);
            this.LostKeyboardFocus += new KeyboardFocusChangedEventHandler(MainWindow_LostKeyboardFocus);
            
            Messenger.Default.Register<bool>(this, "MINIMIZE_TO_TRAY", p => MinimizeToTray());
            Messenger.Default.Register<bool>(this, "QUIT_APPLICATION", p => CloseApp());
            Messenger.Default.Register<bool>(this, "TELL_ME_MORE_CLICKED", p => LaunchLink());

            Closing += (s, e) => ViewModelLocator.Cleanup();
        }

        void MainWindow_Activated(object sender, System.EventArgs e)
        {
            btnStart.Focus();
        }

        void MainWindow_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            this.Topmost = true;
        }


        private void InitializeAppTimer()
        {
            _appTimer.Interval = new System.TimeSpan(0, 20, 0);//Repeats every 20 mins
            _appTimer.IsEnabled = false;
            _appTimer.Tick += new System.EventHandler(_appTimer_Tick);
        }

        void _appTimer_Tick(object sender, System.EventArgs e)
        {
            MyNotifyIcon.Visible = false;
            this.ShowInTaskbar = false;
            this.Visibility = Visibility.Visible;
            _appTimer.IsEnabled = false;
        }

        private void InitializeNotifyIcon()
        {
            MyNotifyIcon = new System.Windows.Forms.NotifyIcon();
            MyNotifyIcon.Icon = new System.Drawing.Icon(@".\Resources\eye.ico");
            MyNotifyIcon.BalloonTipTitle = "Principle20-20-20";
            MyNotifyIcon.BalloonTipText = "Next pop up in 20 mins";
            MyNotifyIcon.ShowBalloonTip(400);            
        }

        private void MinimizeToTray()
        {
            MyNotifyIcon.Visible = true;
            this.ShowInTaskbar = false;
            this.Visibility = Visibility.Hidden;
            _appTimer.IsEnabled = true;            
        }

        private void CloseApp()
        {
            if(System.Windows.MessageBox.Show("Are you sure you want to quit ?", "Eye Strain Relief Tool", MessageBoxButton.YesNo).Equals(MessageBoxResult.Yes))
                this.Close();
        }

        private void LaunchLink()
        {
            try
            {
                Process.Start(new ProcessStartInfo("http://www.webmd.com/eye-health/ss/slideshow-healthier-eyes"));
            }
            catch { }
        }
    }
}